<?php

	/**
	 * @desc Mark that some function is deprecated and terminate
	 * the programe.
	 */
	function deprecated($fn=""){
		SQL::markError();
		throw new Exception("Function $fn is deprecated");	
	}

	
    /**
     * @desc Return the first element of an array. If
     * the array is not having the first element, return null.
     */
    function first($arr){
        if (count($arr)<=0){
            return null;
        }
        return $arr[0];
    }
    
    
    /**
    * @desc Mark a note when writing page for debug purpose.
    */
	function mark($str){
		// if (DEBUG_MODE>0)
		if (is_array($str) || is_object($str)){
			print_r($str);
		}
        else{
			echo "[<b color='red'>".$str."</b>]";
		}
	}
	
    
    /**
    * @desc Print all information about the debug_backtrace().
    */
	function debuginfo(){
		return "<pre>".print_r(debug_backtrace(),true)."</pre>";
	}

    
    /**
    * @desc Trigger an error.
    */
	function error($message){
		if (DEBUG_MODE==0){
			echo "Error: $message";
		}
		
		if (DEBUG_MODE>0){
			$debug=debuginfo();
			echo "<pre>\n{$debug}\n</pre>";
			Log::save($debug,"unexpected/debug.txt");
		}
	}

	/**
	 * @desc Trigger an error, and terminate
	 */
	function fatal($error){
		throw new Exception($error);
		Joob_Utils_Log::save(debuginfo(),"unexpected/debug.txt");
	}
	
	
	/**
	 * @desc Confirm that some condition must be true, or
	 * terminate the program.
	 * @param $cond The condition to be evaluated.
	 * @param $message The printed message on error.
	 */
	function confirm($cond,$message=''){
		if (!$cond){
			throw new Exception($message);
		}
	}

	
	/**
	 * @desc Make seed for the randomness.
	 */
	function make_seed(){
		list($usec, $sec) = explode(' ', microtime());
		return (float) $sec + ((float) $usec * 100000);
	}
	
	
	/**
	 * @desc Get a random value from a distribution $D.
	 * Here $D is a fixed array. So, randDist({2,3,4}) will return  
	 * 0 with 2/9 chance, 1 with 3/9 chance, and 4 with 5/9 chance. 
	 */
	function randDist($D){
		$s=array_sum($D);
		$r=rand(1,$s*1000)/1000.0; # Enlarge the range.
		
		$result=0;
		$range=$D[0];
		$overflow=1000;
		$len=count($D);
		
		while (true){
			if ($r<=$range) return $result;
			$result++;
			$range=$range+$D[$result];
			if ($result>=$overflow) return -1;
			if ($result>=$len) return -1;
		}
	}
	
	
	/**
	 * @desc Return the number if nonnegative, or 0 otherwise.
	 * @param $x The number
	 */
	function nonnegative($x){
		if ($x<=0) return 0;
		return $x;
	}
	
	
	/**
	 * @desc Format the number with its sign.
	 * @param $x The number
	 */
	function signnum($x){
		if ($x>0) return "+{$x}";
		if ($x<0) return "-{$x}";
		return 0;
	}
	

	
	function redirect($url,$base=true){
		if (Joob_Utils_Word::initial($url,4)!='http'){
            $url="http://".$url;
        }
		header("Location: $url");
		exit();
	}
	
	
	function getIP(){
		if (!empty($_SERVER['HTTP_CLIENT_IP'])){   //check ip from share internet
			$ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){   //to check ip is pass from proxy
			$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else{
			$ip=$_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
    
   
?>